﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Defilade
{
    /// <summary>
    /// Class that represents the location on a map (in metres)
    /// </summary>
    [Serializable]
    public class MapLocation
    {
        /// <summary>
        /// Default Location [0.0.0, 0.0.0]
        /// </summary>
        public MapLocation()
        {
            Latitude = new Latitude();
            Longitude = new Longitude();
        }

        /// <summary>
        /// Builds a Location from string indicating the latitude and longitude. The strings
        /// must be in the format 'DD.MM.SS' (for both Latitude and Longitude).
        /// </summary>
        /// <param name="latitudeString">the string representing the latitude ("DD.MM.SS")</param>
        /// <param name="longitudeString">the string representing the longitude ("DD.MM.SS")</param>
        public MapLocation(String latitudeString, String longitudeString)
        {
            Latitude = new Latitude(latitudeString);
            Longitude = new Longitude(longitudeString);
        }

        /// <summary>
        /// The latitude of the location (in meters)
        /// </summary>
        public Latitude Latitude
        {
            get;
            set;
        }

        /// <summary>
        /// The longitude of the location (in meters)
        /// </summary>
        public Longitude Longitude
        {
            get;
            set;
        }

        /// <summary>
        /// Calculates the distance between this location and another location.
        /// </summary>
        /// <param name="otherLocation">the location to calculate the distance to</param>
        /// <returns>the distance (in meters) between the two locations</returns>
        public double DistanceTo(MapLocation otherLocation)
        {
            //Hooray for pythagoras
            double latDiff = Latitude.Metres - otherLocation.Latitude.Metres;
            double lonDiff = Longitude.Metres - otherLocation.Longitude.Metres;

            return Math.Sqrt((latDiff * latDiff) + (lonDiff * lonDiff));
        }

        public override string ToString()
        {
            return String.Format("[{0}, {1}]", Latitude.ToString(), Longitude.ToString());
        }
    }
}
